package com.example.yuncang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yuncang.common.QueryPageParam;
import com.example.yuncang.common.Result;
import com.example.yuncang.entity.Goods;
import com.example.yuncang.entity.Record;
import com.example.yuncang.entity.User;
import com.example.yuncang.service.GoodsService;
import com.example.yuncang.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @Autowired
    private GoodsService goodsService;
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Record record){
        Goods goods = goodsService.getById(record.getGoods());
        int n=record.getCount();

        //出库
        if("2".equals(record.getAction())){
           n=-n;
           record.setCount(n);
        }
        int num = goods.getCount()+n;
        goods.setCount(num);
        goodsService.updateById(goods);
        return recordService.save(record)?Result.suc():Result.fail();
    }
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String goodstype=(String)param.get("goodstype");
        String enterprise=(String)param.get("enterprise") ;
        String role=(String)param.get("role");
        String userId=(String)param.get("userId") ;

        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

       QueryWrapper<Record> queryWrapper = new QueryWrapper();
        queryWrapper.apply("a.goods=b.id and b.enterprise=c.id and b.goodsType=d.id");

        if("1".equals((role))){
            queryWrapper.apply("a.userId="+userId);
        }


        if(!StringUtils.isEmpty(name) && !"null".equals(name)){
           // lambdaQueryWrapper.like(Record::getName,name);
            queryWrapper.like("b.name",name);
        }
        if(!StringUtils.isEmpty(enterprise) && !"null".equals(enterprise)){
         queryWrapper.eq("c.id",enterprise);
        }
        if(!StringUtils.isEmpty(goodstype) && !"null".equals(goodstype)){
            queryWrapper.eq("d.id",goodstype);
        }



        IPage result = recordService.pageCC(page,queryWrapper);

        System.out.println("total=="+result.getTotal());

        return Result.suc(result.getRecords(),result.getTotal());
    }
}
